import axios from "axios";
import { fromJS } from "immutable";
import * as actionTypes from './actionTypes'

const changeHomeData = (result)=>({
    type:actionTypes.CHANGE_HOME_PAGE,
    articleList:result.articleList,
    recommendList:result.recommendList
})

const addHomeList = (list,nextPage)=>({
    type:actionTypes.ADD_HOME_LIST,
    list:fromJS(list),
    nextPage
})

export const getHomeInfo = ()=>{
    return (dispatch)=>{
        axios.get('/api/home.json').then((res)=>{
            const result = res.data.data;
            const action = changeHomeData(result)
            dispatch(action)
        }).catch(()=>{

        })
    }
}

export const getMoreList = (page)=>{
    return (dispatch)=>{
        axios.get('/api/homeList.json?page='+page).then((res)=>{
            const result = res.data.data;
            dispatch(addHomeList(result,page+1))
        }).catch(()=>{

        })
    }
}

export const toggleTopShow = (show)=>({
    type:actionTypes.TOGGLE_SCROLL_SHOW,
    show
})

